<?php
require_once($_SERVER['DOCUMENT_ROOT'] . "/public/BaseCms.php");
require_once($_SERVER['DOCUMENT_ROOT'] . "/public/Attachment.php");

/**
 * Class Information
 */
class Information extends BaseCms
{
    public $tb_name = "systemconfig";

    function __construct()
    {
        parent::__construct($this->tb_name);
    }

    /**
     * 获取系统信息
     */
    function getSystemInfo()
    {
        return $this->findOne();
    }

    /**
     * 获取系统信息
     */
    function getDefaultSystemInfo()
    {
        $cond = ["is_default_show" => 1];
        return $this->findOne($cond);
    }

    /**
     * 备份数据库
     * @param $path
     * @param int $type 1 全量备份 2 备份表结构 3 备份数据
     */
    public function backup($type = 1)
    {
        $path = $_SERVER["DOCUMENT_ROOT"] . "/backup/";
        if (!file_exists($path)) {
            mkdir($path);
        }
        $attach = new Attachment();
        $file_name = $this->db->backup($path, (int)$type);
        $data = ["file_name" => $file_name, "file_path" => $path . $file_name, "business_no" => "db"];
        $attach->merge($attach->getPrimaryKey(), $data);
    }

    /**
     * 还原数据库
     * @param $file_name 文件名
     * @param int $type 1 全量还原 2 只还原数据
     */
    public function restore($file_name, $type = 2)
    {
        $this->db->restore($file_name, (int)$type);
    }

    /**
     * 网站静态化
     * @param $type 1 首页静态化 2 全站静态化
     */
    public function html($type)
    {
        require_once $_SERVER['DOCUMENT_ROOT'] . "/common/HtmlOperation.php";
//        require_once $_SERVER['DOCUMENT_ROOT'] . "/common/WebSocket.php";
//        $ip = $_SERVER['SERVER_ADDR'];
//        $port = $_SERVER["SERVER_PORT"];
//
//        error_log($ip.":".$port);
        $html = new HtmlOperation();
        error_log("xxxxxx" . $type);
        if ((int)$type === 1) {
            $html->index();
        } else {
            $html->all();
        }
//        $socket = new WebSocket($ip, $port, 1000);
//        $socket->start();

    }

    /**
     * 加载备份文件
     */
    public function loadDbFile()
    {
        $attach = new Attachment();
        return $attach->findList(["business_no" => "db"], ["created_date" => "desc"]);
    }

}